﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Micronet.OpenWeb.Com.BLL;
using Micronet.OpenWeb.Com.Controls;
using Micronet.OpenWeb.Utils;

namespace Micronet.OpenWeb.OpenWeb.Admin.Views.Shared
{
    public partial class HomeProfileDetailsAdmin : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //foreach (var owOpenHomeProfileDetailsEntity in OwOpenHomeProfileDetailsBLL.OwGetAll(null, null, null, null))
            //{
            //    owOpenHomeProfileDetailsEntity.IsDefault = false;
            //    owOpenHomeProfileDetailsEntity.IsDeleted = false;
            //    OwOpenHomeProfileDetailsBLL.OwSave(owOpenHomeProfileDetailsEntity);
            //}
        }

        protected void IsDefaultCbb_CheckedChanged(object sender, EventArgs e)
        {
            var IsDefaultcbb = (OwCheckBox)sender;
            if (IsDefaultcbb.Checked.ToString().OwToBoolean() == true)
            {
                var ods = OwOpenHomeProfileDetailsBLL.OwGetByDynamic(null, null, null, null);
                var en = OwOpenHomeProfileDetailsBLL.OwGetEntity(IsDefaultcbb.Text.Trim().OwToInt64TryParse());

                foreach (var entity in ods)
                {

                    if (entity.OpenHomeProfileDetailID != en.OpenHomeProfileDetailID)
                    {
                        entity.IsDefault = false;
                        OwOpenHomeProfileDetailsBLL.OwSave(entity);
                    }
                    else
                    {
                        
                        entity.IsDefault = true;
                        OwOpenHomeProfileDetailsBLL.OwSave(entity);

                        
                    }
                }
            }
            else
            {
                var en = OwOpenHomeProfileDetailsBLL.OwGetEntity(IsDefaultcbb.Text.Trim().OwToInt64TryParse());
                en.IsDefault = false;
                OwOpenHomeProfileDetailsBLL.OwSave(en);
            }
            HomeProfileDetailsdv.Rebind();
        }
        
        protected void IsDeletedCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            var isDeletedCheckBox = (OwCheckBox)sender;
            var obj = OwOpenHomeProfileDetailsBLL.OwGetEntity(isDeletedCheckBox.Text.Trim().OwToInt64TryParse());
            obj.IsDeleted = isDeletedCheckBox.Checked.ToString().OwToBoolean();
            if (OwOpenHomeProfileDetailsBLL.OwSave(obj) > 0)
            {
                HomeProfileDetailsdv.Rebind();
            }
        }
    }
}